//
//  NSString+Add.m
//  YSProject
//
//  Created by 昱胜 on 2022/6/22.
//

#import "NSString+Add.h"

#import "HNSH_GoodsListViewController.h"
#import "HNSH_GoodsDetailViewController.h"
#import "HNSH_ExchangeViewController.h"
#import "HNSH_PackZQViewController.h"

@implementation NSString (Add)

+ (NSString *)appName {
    NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
    NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
    return app_Name;
}

+ (BOOL)judgeString:(NSString *)string {
    NSString * regex =@"[A-Za-z]+";
    NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return[predicate evaluateWithObject:string];
}

//MARK: 手机号码合法性判断
+ (BOOL)isValidateMobileWithPhone:(NSString *)phone{
    NSString * regex = @"^[1][3-9][0-9]{9}$";
    NSPredicate * phoneNum = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [phoneNum evaluateWithObject:phone];
}

/** 手机号加* */
+ (NSString *)transPhoneWithMobile:(NSString *)mobile {
    if (mobile && mobile.length == 11) {
        mobile = [mobile stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
    }
    return mobile;
}

/** 身份证号加* */
+ (NSString *)transIdCardWithIdCard:(NSString *)idCard {
    if (idCard && idCard.length == 18) {
        idCard = [idCard stringByReplacingCharactersInRange:NSMakeRange(4, 12) withString:@"************"];
    }
    return idCard;
}

//MARK: 获取当前日期
+ (NSString *)getToday{
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate * datenow = [NSDate date];
    NSString * currentTimeString = [formatter stringFromDate:datenow];
    return currentTimeString;
}

//MARK: 获取当前日期
+ (NSString *)getTodayWithFormatter:(NSString *)form {
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:form];
    NSDate * datenow = [NSDate date];
    NSString * currentTimeString = [formatter stringFromDate:datenow];
    return currentTimeString;
}

/** 富文本文字 */
+ (NSAttributedString *)transAttWithSting:(NSString *)string{
    if (![string isNotBlank]) {
        return nil;
    }
    NSAttributedString * att = [[NSAttributedString alloc] initWithData:[string dataUsingEncoding:NSUnicodeStringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType} documentAttributes:nil error:nil];
    return att;
}

/** 等级转文字* */
+ (NSString *)getLevelNameWithLevel:(NSInteger)level {
    NSArray *ary = @[@"农户",@"一星",@"二星",@"三星",@"四星",@"五星"];
    if (level >= ary.count) {
        return @"农户";
    }
    return ary[level];
}

/** 获取当前时间戳 */
+ (NSString *)getNowTimeTimestamp {
    NSDate * datenow = [NSDate date];
    NSString * timeSp = [NSString stringWithFormat:@"%ld", (long)([datenow timeIntervalSince1970] * 1000)];
    return timeSp;
}

//聊天获取扩展
+ (NSString *)getExtraString {
    ST_UserInfo * model = [ST_UserManager shareInstance].user;
    NSDictionary * dic = @{@"userId":model.uid ? : @"", @"phone":model.phone ? : @"", @"creditLevel":@"0", @"portrait":[ST_UserManager shareInstance].rcUser.portrait};
    NSString * extra = [dic jsonStringEncoded];
    return extra;
}


- (void)copyString {
    UIPasteboard *pab = [UIPasteboard generalPasteboard];
    pab.string = self;
}

//对比两个版本
+ (BOOL)isHaveNewVersion:(NSString *)newVersion {
    NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    currentVersion = [currentVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
    NSString *newestVersion = [newVersion stringByReplacingOccurrencesOfString:@"." withString:@""];
    
    return [newestVersion integerValue] > [currentVersion integerValue];
}

/**
 * 将中文进行UTF-8编码
 * @param content 预编码的中文
*/
+ (NSString *)getNSUTF8String:(NSString *)content{
//    NSLog(@"原url:%@", content);
    NSString *encodedString = (NSString *)
    CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                              
                                                              (CFStringRef)content,
                                                              
                                                              (CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]",
                                                              
                                                              NULL,
                                                              
                                                              kCFStringEncodingUTF8));
    
//    NSLog(@"转码url:%@",  encodedString);
    return encodedString;
}

//获取小时
+ (NSString *)getTimeHourWithTime:(NSInteger)time {
    return [NSString stringWithFormat:@"%02ld",(time/3600)%24];
}
//获取分钟
+ (NSString *)getTimeMinuteWithTime:(NSInteger)time {
    return [NSString stringWithFormat:@"%02ld",(time%3600)/60];
}
//获取秒
+ (NSString *)getTimeSecondWithTime:(NSInteger)time {
    return [NSString stringWithFormat:@"%02ld",time%60];
}


/** 金钱每三位加一个逗号, 小数点省略到两位*/
+(NSString *)countNumAndChangeformat:(NSString *)num
{
    if([num rangeOfString:@"."].location !=NSNotFound)  {
        NSString *losttotal = [NSString stringWithFormat:@"%.2f",[num doubleValue]];//小数点后只保留两位
        NSArray *array = [losttotal componentsSeparatedByString:@"."];
        //小数点前:array[0]
        //小数点后:array[1]
        int count = 0;
        num = array[0];
        long long int a = num.longLongValue;
        while (a != 0)
        {
            count++;
            a /= 10;
        }
        NSMutableString *string = [NSMutableString stringWithString:num];
        NSMutableString *newstring = [NSMutableString string];
        while (count > 3) {
            count -= 3;
            NSRange rang = NSMakeRange(string.length - 3, 3);
            NSString *str = [string substringWithRange:rang];
            [newstring insertString:str atIndex:0];
            [newstring insertString:@"," atIndex:0];
            [string deleteCharactersInRange:rang];
        }
        [newstring insertString:string atIndex:0];
        NSMutableString *newString = [NSMutableString stringWithFormat:@"%@.%@",newstring,array[1]];
        return newString;
    }else {
        int count = 0;
        long long int a = num.longLongValue;
        while (a != 0)
        {
            count++;
            a /= 10;
        }
        NSMutableString *string = [NSMutableString stringWithString:num];
        NSMutableString *newstring = [NSMutableString string];
        while (count > 3) {
            count -= 3;
            NSRange rang = NSMakeRange(string.length - 3, 3);
            NSString *str = [string substringWithRange:rang];
            [newstring insertString:str atIndex:0];
            [newstring insertString:@"," atIndex:0];
            [string deleteCharactersInRange:rang];
        }
        [newstring insertString:string atIndex:0];
        return newstring;
    }
}

/** 金钱每三位加一个逗号, 小数点省略到两位, 无小数点则添加两位小数*/
+ (NSString *)floatMoneyNumAndChangeformat:(NSString *)num {
    NSString *fomatterStr = [NSString countNumAndChangeformat:num];
    if([fomatterStr rangeOfString:@"."].location !=NSNotFound) {
        return fomatterStr;
    }
    return [NSString stringWithFormat:@"%@.00", fomatterStr];
}


+ (NSString *)translation:(NSInteger)arebic {
    NSString *numString = @"";
    NSArray *chinese_numerals = @[@"零",@"一",@"二",@"三",@"四",@"五",@"六",@"七",@"八",@"九",@"十"];
    numString = chinese_numerals[arebic>chinese_numerals.count ? chinese_numerals.count-1 : arebic];
    return numString;
}

+ (void)jumpToolsWithJumpType:(NSInteger)jump_type
                    jump_name:(NSString *)jump_name
                 jump_content:(NSString *)jump_content
                  jump_params:(NSString *)jump_params {
    if (jump_type == 0) {//无跳转
        
    } else if (jump_type == 1) {//图片预览

    } else if (jump_type == 2) {//网页地址
        ST_BaseWebController * vc = [ST_BaseWebController new];
        vc.titleString = jump_name;
        [vc loadURLString:jump_content];
        [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
    } else if (jump_type == 3) {//视频

    } else if (jump_type == 4) {//跳转内部
        if ([jump_content isEqualToString:@"goods_list_mcate"]) {//首页分类跳转
            HNSH_GoodsListViewController * vc = [HNSH_GoodsListViewController new];
            vc.mcate_id = jump_params;
            [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
        } else if ([jump_content isEqualToString:@"goods_detail"]) {//详情
            HNSH_GoodsDetailViewController * vc = [HNSH_GoodsDetailViewController new];
            vc.goodsId = jump_params;
            [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
        } else if ([jump_content isEqualToString:@"home_main_jxzq"]) {//精选专区
            HNSH_GoodsListViewController * vc = [HNSH_GoodsListViewController new];
            vc.mcate_id = jump_params;
            [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
        } else if ([jump_content isEqualToString:@"goods_list_gift_pack"]) {//礼包专区
            HNSH_PackZQViewController * vc = [HNSH_PackZQViewController new];
            vc.type = @"10";
            [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
        } else if ([jump_content isEqualToString:@"goods_list_hdzq"]) {//活动专区
            HNSH_PackZQViewController * vc = [HNSH_PackZQViewController new];
            vc.type = @"6";
            [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
        }
    } else if (jump_type == 5) {//抖音直播  

    }
}

@end
